#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <iostream>

int main() {
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(8080);
    inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);

    connect(sockfd, (struct sockaddr*)&server, sizeof(server));
    while(true)
    {
        // 发送 0 字节数据
        send(sockfd, "1", 1, 0);
        printf("发送 1 字节数据...\n");
        char buf[1024] = {0};
        int ret = recv(sockfd,buf,1024,0);
        if(ret<0)
        {
            perror("recv error");
            close(sockfd);
            break;
        }
        else if(ret == 0)
        {   
            perror("peer shutdown!");
            close(sockfd);
            break;
        }
        std::cout<<buf<<std::endl;
    }
    // 关闭连接
    close(sockfd);
    return 0;
}
